/**
 * 作者: wgx
 * 日期: 2025-05-14 14:13:20
 * 备注: 用户大世界行军模块的伙伴派遣数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserWorldPartner;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserWorldPartnerModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.partner_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 数据库ID
    protected long userId; // 用户ID
    protected int partnerId; // partner_id
    protected int teamId; // 队伍id，0表示空闲

    public void reset() {
    }

    protected String getKey() {
        return "UserWorldPartner." + userId + "." + partnerId;
    }

    protected String getListKey() {
        return "UserWorldPartner." + userId;
    }

    protected String getListItemKey() {
        return partnerId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_world_partner"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 数据库ID
        obj.put("userId", userId); // 用户ID
        obj.put("partnerId", partnerId); // partner_id
        obj.put("teamId", teamId); // 队伍id，0表示空闲
        return obj;
    }

    public PBUserWorldPartner.Builder takeInitBuilder() {
        PBUserWorldPartner.Builder sb = PBUserWorldPartner.newBuilder();
        if (partnerId != 0) {
            sb.setPartnerId(partnerId);
        }
        if (teamId != 0) {
            sb.setTeamId(teamId);
        }
        return sb;
    }

    public PBUserWorldPartner.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserWorldPartner.Builder sb = PBUserWorldPartner.newBuilder();
        if (isFieldUpdate("partnerId")) {
            sb.setPartnerId(partnerId);
        }
        if (isFieldUpdate("teamId")) {
            sb.setTeamId(teamId);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getPartnerId() {
        return partnerId;
    }

    public void putPartnerId(int partnerId) {
        if (this.partnerId != partnerId) {
            this.partnerId = partnerId;
            updateRedisHashField(getKey(), "partnerId", this.partnerId);
        }
    }

    public void addPartnerId(int value) {
        this.partnerId += value;
        updateRedisHashField(getKey(), "partnerId", this.partnerId);
    }

    public int getTeamId() {
        return teamId;
    }

    public void putTeamId(int teamId) {
        if (this.teamId != teamId) {
            this.teamId = teamId;
            updateRedisHashField(getKey(), "teamId", this.teamId);
        }
    }

    public void addTeamId(int value) {
        this.teamId += value;
        updateRedisHashField(getKey(), "teamId", this.teamId);
    }

}